package com.fenghongzhang.day005;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


/**
 * A simple {@link Fragment} subclass.
 */
public class UpFragment extends Fragment {
    private EditText upEdit;
    private Button upBtn;
    public UpFragment() {
        // Required empty public constructor
    }

    //定义接口
    public interface UpFGListener{
        void send(String s);
    }
    //2,声明接口
    private UpFGListener upFGListener;
    //3赋值


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_up, container, false);
        upEdit = (EditText) inflate.findViewById(R.id.up_edit);
        upBtn = (Button) inflate.findViewById(R.id.up_btn);


        //fg的嵌套.必须是getChildFragmentManager()
        FragmentManager childFragmentManager = getChildFragmentManager();

        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fg_ll,new DownFragment());
        fragmentTransaction.commit();


        //通过id找.静态用idfindFragmentById()
        //动态用tag
//        upFGListener = (UpFGListener) getActivity().getSupportFragmentManager().findFragmentByTag("down");

        upBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = upEdit.getText().toString();
                upFGListener.send(string);
            }
        });

        return inflate;
    }
}
